/** */
package com.smc.sys.security;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.smc.common.utils.StringUtils;
import com.smc.sys.model.User;

/**
 * 系统上下文信息
 *
 * @author zouqinghua
 * @date 2016年9月6日 上午11:48:25
 */
public class AppContext {

  private static final Logger logger = LoggerFactory.getLogger(AppContext.class);

  public static final String AUTH_NAME = "Authorization";
  public static final String USER_INFO_NAME = "user";
  public static final String SERVER_NAME = "teacher";
  public static final String SERVER_NAME_SPLIT = " ";

  public static final String USER_ID = "userId";
  public static final String USER_LOGIN_NAME = "username";
  public static final String USER_SHOW_NAME = "name";
  public static final String USER_PASSWORD = "password";
  public static final String USER_TOKEN = "token";
  public static final String APP_TOKEN = "appToken";
  
  public static final String REMOVE_IP = "remoteIP";
  public static final String TRACE_IP = "traceId";
  
  
  private static ThreadLocal<Map<String, Object>> context = new ThreadLocal<Map<String, Object>>();

  public static Map<String, Object> getContext() {
    if (context == null) {
      context = new ThreadLocal<Map<String, Object>>();
    }
    if (context.get() == null) {
      context.set(Maps.newHashMap());
    }
    return context.get();
  }

  public static void setContext(Map<String, Object> context) {
    AppContext.context.set(context);
  }

  
  public static void setUser(User user) {
    put(USER_INFO_NAME, user);
  }

  public static void setAuthorization(String authorization) {
    put(AUTH_NAME, authorization);
  }

  public static void setRemoteIP(String remoteIP) {
	    put(REMOVE_IP, remoteIP);
	  }
  public static void setTraceId(String traceId) {
	    put(TRACE_IP, traceId);
	  }
  
  public static String getRemoteIP(){
	  return (String) get(REMOVE_IP);
  }
  public static String getTraceId(){
	  return (String) get(TRACE_IP);
  }
  
  public static User getUser() {
    User user = (User) get(USER_INFO_NAME);
    return user;
  }

  public static String getAuthorization() {
    String authorization = (String) get(AUTH_NAME);
    if (StringUtils.isBlank(authorization)) {
      User user = getUser();
      if (user != null && user.getId()!=null && user.getId() > 0 && !StringUtils.isNoneBlank(user.getToken())) {
        String[] authArray =
            new String[] {SERVER_NAME, String.valueOf(user.getId()), user.getToken()};
        authorization = StringUtils.join(authArray, SERVER_NAME_SPLIT);
      }
    }
    return authorization;
  }

  public static void put(String key, Object value) {
    getContext().put(key, value);
  }

  public static Object get(String key) {
    return getContext().get(key);
  }

  public static String getToken() {
    if (getUser() != null) {
      return getUser().getToken();
    }
    return null;
  }

  public static void setAppToken(String appToken) {
    put(APP_TOKEN, appToken);
  }

  public static String getAppToken() {
    Object obj = get(APP_TOKEN);
    return obj == null ? null : obj.toString();
  }

  public static void releaseAppResource() {
    try {
      context.remove();
    } catch (Exception e) {
    }
  }
}
